package com.example.myapp.ADO;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import com.example.myapp.Activity.ArticleBody;
import com.example.myapp.Activity.Friendshome;
import com.example.myapp.Activity.Myhome;
import com.example.myapp.Model.User;
import com.example.myapp.R;
import com.example.myapp.Model.Article;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.datatype.BmobRelation;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import de.hdodenhof.circleimageview.CircleImageView;

public class Mini_Adapter extends RecyclerView.Adapter<Mini_Adapter.ViewHolder>  {

    private LayoutInflater mInflater;
    private List<Article> postsList;
    private Context context;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public Mini_Adapter(Context context, List<Article> postsList) {
        this.postsList=postsList;
        this.context=context;
        this. mInflater = LayoutInflater.from(context);//初始化
    }
    static class ViewHolder extends RecyclerView.ViewHolder{

        TextView postdetails_content;
        TextView artlist_username;
        CircleImageView icon;
        TextView artlist_time;
        LinearLayout oneimg;
        ImageView oneimg1;
        LinearLayout twoimg;
        ImageView twoimg1;
        ImageView twoimg2;
        LinearLayout fourimg;
        ImageView fourimg1;
        ImageView fourimg2;
        ImageView fourimg3;
        ImageView fourimg4;
        TextView artlist_zhuanfapinlun;
        LinearLayout artlist_body;
        public ViewHolder (View view)
        {
            super(view);
            artlist_zhuanfapinlun=view.findViewById(R.id.artlist_zhuanfapinlun);
            artlist_body=view.findViewById(R.id.artlist_body);

            artlist_username=view.findViewById(R.id.artlist_username);
             icon = view.findViewById(R.id.artlist_icon);
            postdetails_content= view.findViewById(R.id.postdetails_content);
            artlist_time= view.findViewById(R.id.artlist_time);
            oneimg=view.findViewById(R.id.oneimg);
            oneimg1=view.findViewById(R.id.oneimg1);
            twoimg=view.findViewById(R.id.twoimg);
            twoimg1=view.findViewById(R.id.twoimg1);
            twoimg2=view.findViewById(R.id.twoimg2);
            fourimg=view.findViewById(R.id.fourimg);
            fourimg1=view.findViewById(R.id.fourimg1);
            fourimg2=view.findViewById(R.id.fourimg2);
            fourimg3=view.findViewById(R.id.fourimg3);
            fourimg4=view.findViewById(R.id.fourimg4);

        }

    }



    @Override

    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.mini_articlelist,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int i) {


        /**如果文章带转发品论的*/
        if (postsList.get(i).getZhuanfaContent() != null) {
            holder.artlist_zhuanfapinlun.setVisibility(View.VISIBLE);
            holder.artlist_zhuanfapinlun.setText(postsList.get(i).getZhuanfaContent());
            //content.setText(list.get(i).getContent());
            String[] split = postsList.get(i).getContent().split(":");
            String str1 = "<font color = '#6795B5'>" + split[0] + ":" + "</font>" + split[1] + "";
            CharSequence charSequence;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                charSequence = Html.fromHtml(str1, Html.FROM_HTML_MODE_LEGACY);
            } else {
                charSequence = Html.fromHtml(str1);
            }
            holder.artlist_body.setBackgroundColor(Color.parseColor("#EEEEEE"));
            holder.postdetails_content.setText(charSequence);
            /**如果文章是原著*/
        } else {
            holder.postdetails_content.setText(postsList.get(i).getContent());
        }

        holder.artlist_username.setText(postsList.get(i).getAuthor().getUsername());
        Glide.with(context).load(postsList.get(i).getAuthor().getIcon().getUrl()).into(holder.icon);// CircleImageView.显示网络图片特殊方法
        holder. artlist_time.setText(postsList.get(i).getCreatedAt());
        //如果没用图片
        if (postsList.get(i).getImages() == null) {
            holder. oneimg.setVisibility(View.GONE);
            holder.fourimg.setVisibility(View.GONE);
        }
        //只有一张图片
        else if (postsList.get(i).getImages2() == null && postsList.get(i).getImages3() == null) {
            holder. oneimg.setVisibility(View.VISIBLE);
            holder. fourimg.setVisibility(View.GONE);
            Glide.with(context).load(postsList.get(i).getImages().getUrl()).into(holder.oneimg1);
        } else if (postsList.get(i).getImages2() != null && postsList.get(i).getImages4() == null) {
            //   2张或三张图片
            holder. twoimg.setVisibility(View.VISIBLE);
            holder. fourimg.setVisibility(View.GONE);
            Glide.with(context).load(postsList.get(i).getImages().getUrl()).into(holder.twoimg1);
            Glide.with(context).load(postsList.get(i).getImages2().getUrl()).into(holder.twoimg2);
        } else {
            Glide.with(context).load(postsList.get(i).getImages().getUrl()).into(holder.fourimg1);
            Glide.with(context).load(postsList.get(i).getImages2().getUrl()).into(holder.fourimg2);
            Glide.with(context).load(postsList.get(i).getImages3().getUrl()).into(holder.fourimg3);
            Glide.with(context).load(postsList.get(i).getImages4().getUrl()).into(holder.fourimg4);
        }


        holder. icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!BmobUser.isLogin()){
                    Toast.makeText(context, "请登陆后再操作！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if ( postsList.get(i).getAuthor().getObjectId().equals(BmobUser.getCurrentUser(User.class).getObjectId())){
                    Intent intent = new Intent(context, Myhome.class);
                    context.startActivity(intent);
                }else {
                    Intent intent = new Intent(context, Friendshome.class);
                    intent.putExtra("userID", postsList.get(i).getAuthor().getObjectId());
                    intent.putExtra("iconUrl",postsList.get(i).getAuthor().getIcon().getUrl());
                    intent.putExtra("userName",postsList.get(i).getAuthor().getUsername());
                    if (postsList.get(i).getAuthor().getShow()!=null){
                        intent.putExtra("jianJie",postsList.get(i).getAuthor().getShow());
                    }
                    context.startActivity(intent);
                }

            }
        });




        holder. artlist_username.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!BmobUser.isLogin()){
                    Toast.makeText(context, "请登陆后再操作！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if ( postsList.get(i).getAuthor().getObjectId().equals(BmobUser.getCurrentUser(User.class).getObjectId())){
                    Intent intent = new Intent(context, Myhome.class);
                    context.startActivity(intent);
                }else {
                    Intent intent = new Intent(context, Friendshome.class);
                    intent.putExtra("userID", postsList.get(i).getAuthor().getObjectId());
                    intent.putExtra("iconUrl",postsList.get(i).getAuthor().getIcon().getUrl());
                    intent.putExtra("userName",postsList.get(i).getAuthor().getUsername());
                    if (postsList.get(i).getAuthor().getShow()!=null){
                        intent.putExtra("jianJie",postsList.get(i).getAuthor().getShow());
                    }
                    context.startActivity(intent);
                }

            }
        });



        /**点击文章主体*/
        holder. artlist_body.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!BmobUser.isLogin()){
                    Toast.makeText(context, "请登陆后再操作！", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(context, ArticleBody.class);
                Log.e("bbbb","我点到的文章id"+ postsList.get(i).getObjectId()+"内容"+ postsList.get(i).getContent());
                intent.putExtra("ArticleID", postsList.get(i).getObjectId());
                context.startActivity(intent);
            }
        });


    }







    @Override
    public int getItemCount(){
        return postsList.size();
    }


}
